package com.ruoyi.zxjd.document.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.zxjd.common.CommonConstants;
import com.ruoyi.zxjd.document.domain.DocumentArrange;
import com.ruoyi.zxjd.document.service.IDocumentArrangeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 公文管理Controller
 * 
 * @author lcc
 * @date 2021-01-28
 */
@Controller
@RequestMapping("/document/arrange")
public class DocumentArrangeController extends BaseController
{
    private String prefix = "document/arrange";

    @Autowired
    private IDocumentArrangeService documentArrangeService;

    @RequiresPermissions("document:arrange:view")
    @GetMapping()
    public String arrange()
    {
        return prefix + "/arrange";
    }

    /**
     * 查询公文管理列表
     */
    @RequiresPermissions("document:arrange:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DocumentArrange documentArrange)
    {
        startPage();
        List<DocumentArrange> list = documentArrangeService.selectDocumentArrangeList(documentArrange);
        return getDataTable(list);
    }

    /**
     * 查询公文管理列表
     */
    @RequiresPermissions("document:arrange:list")
    @PostMapping("/personalList")
    @ResponseBody
    public TableDataInfo personalList(DocumentArrange documentArrange)
    {
        if(documentArrange==null){
            documentArrange = new DocumentArrange();
        }
        documentArrange.setCreateBy(ShiroUtils.getLoginName());
        startPage();
        List<DocumentArrange> list = documentArrangeService.selectDocumentArrangeList(documentArrange);
        return getDataTable(list);
    }

    /**
     * 导出公文管理列表
     */
    @RequiresPermissions("document:arrange:export")
    @Log(title = "公文管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DocumentArrange documentArrange)
    {
        List<DocumentArrange> list = documentArrangeService.selectDocumentArrangeList(documentArrange);
        ExcelUtil<DocumentArrange> util = new ExcelUtil<DocumentArrange>(DocumentArrange.class);
        return util.exportExcel(list, "arrange");
    }

    /**
     * 新增公文管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("today",DateUtils.getDate());
        return prefix + "/add";
    }

    /**
     * 新增保存公文管理
     */
    @RequiresPermissions("document:arrange:add")
    @Log(title = "公文管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DocumentArrange documentArrange,List<MultipartFile> files)
    {


//        documentArrange.setAttachments("69");

        return toAjax(documentArrangeService.insertDocumentArrange(documentArrange));
    }

    /**
     * 修改公文管理
     */
    @GetMapping("/edit/{arrangeId}")
    public String edit(@PathVariable("arrangeId") Long arrangeId, ModelMap mmap)
    {
        DocumentArrange documentArrange = documentArrangeService.selectDocumentArrangeById(arrangeId);
        mmap.put("documentArrange", documentArrange);
        return prefix + "/edit";
    }

    /**
     * 修改保存公文管理
     */
    @RequiresPermissions("document:arrange:edit")
    @Log(title = "公文管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DocumentArrange documentArrange)
    {
        String status = documentArrange.getStatus();
        if(CommonConstants.PUBLISHED.equals(status)){
            return AjaxResult.error("非草稿状态不允许修改！！！");
        }
        return toAjax(documentArrangeService.updateDocumentArrange(documentArrange));
    }

    /**
     * 删除公文管理
     */
    @RequiresPermissions("document:arrange:remove")
    @Log(title = "公文管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(documentArrangeService.deleteDocumentArrangeByIds(ids));
    }

    /**
     * 发布公文
     */
    @RequiresPermissions("document:arrange:release")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/release")
    @ResponseBody
    public AjaxResult release(Long arrangeId)
    {
        return toAjax(documentArrangeService.updateStatusByArrangeId(arrangeId, CommonConstants.ONGOING));
    }

    /**
     * 撤销公文
     */
    @RequiresPermissions("document:arrange:withdraw")
    @Log(title = "工作安排", businessType = BusinessType.UPDATE)
    @PostMapping("/withdraw")
    @ResponseBody
    public AjaxResult withdraw(Long arrangeId)
    {
        return toAjax(documentArrangeService.updateStatusByArrangeId(arrangeId, CommonConstants.DRAFT));
    }

    /**
     * 个人发文
     */
    @GetMapping("/personal")
    public String personalArrange()
    {
        return prefix + "/arrange_personal";
    }


}
